// --*- C++ -*------x-----------------------------------------------------------
//
//
// Author:          Enrico Negri, Alessandro Albiero, Silvio Tosatto
//
// Project name:    BiocompUP Align
//
// Date:            01/2008
//
// Description:     A program for read alignment data, concatenate & print alignments. 
//		    Maximum alignment generator, to be used on MegaBlast output.
//
// -----------------x-----------------------------------------------------------

#include <string>
#include <GetArg.h>
#include <AlignmentBase.h>
#include <Alignment.h>
#include <PdbLoader.h>
#include <PdbSaver.h>
#include <MegaAli.h>

using namespace Biopool;

void sShowHelp()
{
  MegaAli mal;

  cout << "Maximum alignment generator \n"
       << "To be used on MegaBlast output.\n"
       << " Options: \n"
       << "\t-i <filename> \t\t Input MegaBlast -D 3 file\n"
       << "\t-a <filename> \t\t Alignment file (FASTA format)\n"
       << "\t-g <value>    \t\t Maximum gap length (default = " 
       << mal.maxGap << ")\n"
       << "\t-n <value>    \t\t Maximum non-overlap length " 
       << "(default = " << mal.maxIndel << ")\n"
       << "\t-t <value>    \t\t Percentage threshold for accepting suboptimal" 
       << " alignments (default = " << mal.threshold << ")\n" 
       << "\t--verbose <value> \t Verbosity level (default = 0)\n"
       << "\n";
};

void sAddLine()
{
  cout << "-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-\n";
}


int main(int nArgs, char* argv[])
{ 
  // --------------------------------------------------
  // 0. treat options
  // --------------------------------------------------

  MegaAli mal;

  if (getArg( "h", nArgs, argv))
    {
      sShowHelp();
      return 1;
    };

  string inputFile, aliFile;
  getArg( "i", inputFile, nArgs, argv, "!");
  getArg( "a", aliFile, nArgs, argv, "!");
  getArg( "g", static_cast<long int>(mal.maxGap), nArgs, argv, 
	  static_cast<long int>(mal.maxGap));
  getArg( "n", static_cast<long int>(mal.maxIndel), nArgs, argv, 
	  static_cast<long int>(mal.maxIndel));
  getArg( "t", mal.threshold, nArgs, argv, mal.threshold);
  getArg( "-verbose", mal.verbose, nArgs, argv, 0);

  if (inputFile == "!")
    {
      cout << "Missing file specification. Aborting. (-h for help)" << endl;
      return -1;
    }

  // --------------------------------------------------
  // 1. read alignment data
  // --------------------------------------------------

  Alignment ali;

  ifstream aFile(aliFile.c_str());
  if (!aFile)
    ERROR("File not found.", exception);

  ali.loadFasta(aFile);

  ifstream input(inputFile.c_str());
  if (!input)
    ERROR("File not found.", exception);

  // read input data
  mal.readMegaBlastData(input);

  // --------------------------------------------------
  // 2. concatenate & print alignments:
  // --------------------------------------------------

  mal.writeMatrix();

  mal.writeStartingPoints();

  mal.pSortAscending(0, mal.start1.size()-1);

  mal.writeStartingPoints();

  sAddLine();
  mal.concatAlignments(ali);
  mal.writeFragmentAli(ali);
}
